package com.example.logisticsingle.Config;


import com.example.logisticsingle.authorization.AuthProperties;
import com.example.logisticsingle.constants.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger配置类
 *
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {
	public static final String BASE_PACKAGE = "com.rudong.framework.iam";
	@Value("${swagger.enable}")
	private boolean enableSwagger;
	/**
	 * 创建API基本信息
	 * @return
	 */
	@Bean
	public Docket createRestApi(){
//		
//		ParameterBuilder ticketPar = new ParameterBuilder();
//        List<Parameter> pars = new ArrayList<Parameter>();  
//    	ticketPar.name("ticket").description("user ticket")
//    	.modelRef(new ModelRef("string")).parameterType("header") 
//    	.required(false).build(); //header中的ticket参数非必填，传空也可以
//    	pars.add(ticketPar.build());    //根据每个方法名也知道当前方法在设置什么参数
		
		//添加head参数start
    	List<Parameter> pars = new ArrayList<>();
    	ParameterBuilder parameterBuilder1 = new ParameterBuilder();
    	parameterBuilder1.name(AuthProperties.token.header).description("令牌")
				    	.modelRef(new ModelRef("string")).parameterType("header").required(false).build();
    	ParameterBuilder parameterBuilder2 = new ParameterBuilder();
    	parameterBuilder2.name(Config.REQUEST_TYPE).description("请求类型")
				    	.modelRef(new ModelRef("string")).parameterType("header").required(false).build();
    	ParameterBuilder parameterBuilder3 = new ParameterBuilder();
    	parameterBuilder3.name(Config.REQUEST_IP).description("请求IP")
				    	.modelRef(new ModelRef("string")).parameterType("header").required(false).build();
    	ParameterBuilder parameterBuilder4 = new ParameterBuilder();
    	parameterBuilder4.name(Config.REQUEST_USER_AGENT).description("请求UserAgent")
				    	.modelRef(new ModelRef("string")).parameterType("header").required(false).build();
    	pars.add(parameterBuilder1.build());
    	pars.add(parameterBuilder2.build());
    	pars.add(parameterBuilder3.build());
    	pars.add(parameterBuilder4.build());
			
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.enable(enableSwagger)	// 生产环境的时候关闭 swagger 比较安全
				.select()
				.apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE))	// 扫描接口的包路径
				.paths(PathSelectors.any())
				.build()
				.globalOperationParameters(pars);
	}
	/**
	 * 接口信息
	 * @return
	 */
	private ApiInfo apiInfo(){
		return new ApiInfoBuilder()
				.title("API接口服务")
				.description("统一认证服务接口")
				.termsOfServiceUrl("http://www.shrudong.com/")
				.version("1.0.0")
				.build();
	}
}
